package com.calenda.setevent.fragment;

import com.calenda.setevent.R;
import com.calenda.setevent.api.BaseAjaxCallBack.IAjaxCallbackMMK;

import android.annotation.TargetApi; 
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import static com.calenda.setevent.api.SetEventApi.*;
 
public class CreateGroupFragment extends BaseFragment implements IAjaxCallbackMMK {

	private EditText edtNameGroup;
	private EditText edtDesc;
	private TextView tvTao;
	private TextView tvHuy;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.create_group_activity, null);
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onViewCreated(view, savedInstanceState);

		edtNameGroup = (EditText) view.findViewById(R.id.edtNameGroup);
		edtDesc = (EditText) view.findViewById(R.id.edtDesc);

		tvTao = (TextView) view.findViewById(R.id.tvTao);
		tvTao.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String nameGroup = edtNameGroup.getText().toString().trim();
				String desc = edtDesc.getText().toString().trim();

				if (!TextUtils.isEmpty(nameGroup) && !TextUtils.isEmpty(desc)) {
					createGroup(nameGroup, desc, getActivity(),
							CreateGroupFragment.this);
				} else {
					Toast.makeText(getActivity(),
							"Các trường không được để trống !",
							Toast.LENGTH_LONG).show();
				}

			}
		});

		tvHuy = (TextView) view.findViewById(R.id.tvHuy);
		tvHuy.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				edtNameGroup.setText("");
				edtDesc.setText("");
			}
		});
	}

	@Override
	public <T> void handler(T t, int code) {
		// TODO Auto-generated method stub
		String message = (String) t;
		if (code == COMPLETE) {
			Toast.makeText(this.getActivity(),
					"Tạo group thành công: " + message, Toast.LENGTH_LONG)
					.show();
			
			edtNameGroup.setText("");
			edtDesc.setText("");
		} else if (code == ERROR) {
			Toast.makeText(this.getActivity(),
					"Không thể tạo group: " + message, Toast.LENGTH_LONG)
					.show();
		}
	}
}
